﻿using System;
using HslCommunication.Core;

namespace HslCommunication.Profinet.XINJE
{
	/// <summary>
	/// 信捷PLC的相关辅助类
	/// </summary>
	// Token: 0x02000043 RID: 67
	public class XinJEHelper
	{
		// Token: 0x060005B2 RID: 1458 RVA: 0x0001A3D0 File Offset: 0x000185D0
		private static int CalculateXinJEStartAddress(string address)
		{
			bool flag = address.IndexOf('.') < 0;
			int result;
			if (flag)
			{
				result = Convert.ToInt32(address, 8);
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					'.'
				}, StringSplitOptions.RemoveEmptyEntries);
				result = Convert.ToInt32(array[0], 8) * 8 + int.Parse(array[1]);
			}
			return result;
		}

		/// <summary>
		/// 根据信捷PLC的地址，解析出转换后的modbus协议信息，适用XC系列
		/// </summary>
		/// <param name="address">安川plc的地址信息</param>
		/// <param name="modbusCode">原始的对应的modbus信息</param>
		/// <returns>还原后的modbus地址</returns>
		// Token: 0x060005B3 RID: 1459 RVA: 0x0001A424 File Offset: 0x00018624
		public static OperateResult<string> PraseXinJEXCAddress(string address, byte modbusCode)
		{
			OperateResult<string> result;
			try
			{
				string str = string.Empty;
				OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
				bool isSuccess = operateResult.IsSuccess;
				if (isSuccess)
				{
					str = string.Format("s={0};", operateResult.Content);
				}
				bool flag = modbusCode == 1 || modbusCode == 15 || modbusCode == 5;
				if (flag)
				{
					bool flag2 = address.StartsWith("X") || address.StartsWith("x");
					if (flag2)
					{
						return OperateResult.CreateSuccessResult<string>(str + (XinJEHelper.CalculateXinJEStartAddress(address.Substring(1)) + 16384).ToString());
					}
					bool flag3 = address.StartsWith("Y") || address.StartsWith("y");
					if (flag3)
					{
						return OperateResult.CreateSuccessResult<string>(str + (XinJEHelper.CalculateXinJEStartAddress(address.Substring(1)) + 18432).ToString());
					}
					bool flag4 = address.StartsWith("S") || address.StartsWith("s");
					if (flag4)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 20480).ToString());
					}
					bool flag5 = address.StartsWith("T") || address.StartsWith("t");
					if (flag5)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 25600).ToString());
					}
					bool flag6 = address.StartsWith("C") || address.StartsWith("c");
					if (flag6)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 27648).ToString());
					}
					bool flag7 = address.StartsWith("M") || address.StartsWith("m");
					if (flag7)
					{
						int num = Convert.ToInt32(address.Substring(1));
						bool flag8 = num >= 8000;
						if (flag8)
						{
							return OperateResult.CreateSuccessResult<string>(str + (num - 8000 + 24576).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + num.ToString());
					}
				}
				else
				{
					bool flag9 = address.StartsWith("D") || address.StartsWith("d");
					if (flag9)
					{
						int num2 = Convert.ToInt32(address.Substring(1));
						bool flag10 = num2 >= 8000;
						if (flag10)
						{
							return OperateResult.CreateSuccessResult<string>(str + (num2 - 8000 + 16384).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + num2.ToString());
					}
					else
					{
						bool flag11 = address.StartsWith("F") || address.StartsWith("f");
						if (flag11)
						{
							int num3 = Convert.ToInt32(address.Substring(1));
							bool flag12 = num3 >= 8000;
							if (flag12)
							{
								return OperateResult.CreateSuccessResult<string>(str + (num3 - 8000 + 26624).ToString());
							}
							return OperateResult.CreateSuccessResult<string>(str + (num3 + 18432).ToString());
						}
						else
						{
							bool flag13 = address.StartsWith("E") || address.StartsWith("e");
							if (flag13)
							{
								return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 28672).ToString());
							}
							bool flag14 = address.StartsWith("T") || address.StartsWith("t");
							if (flag14)
							{
								return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
							}
							bool flag15 = address.StartsWith("C") || address.StartsWith("c");
							if (flag15)
							{
								return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 14336).ToString());
							}
						}
					}
				}
				result = new OperateResult<string>(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}
	}
}
